// Copyright (c) Microsoft Corporation and Contributors.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Shapes;
using Microsoft.UI.Xaml.Navigation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Microsoft.UI;
using WinTime.Data.Entity;
using WinTime.Models;
using WinTime.ViewModels;
using System.Drawing;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace WinTime.Controls;
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class CategoryModifyDialog : Page
{
    public CategoryModel CurrentCategory;

    public CategoryModifyDialog()
    {
        this.InitializeComponent();
    }

    public CategoryModifyDialog(CategoryModel model)
    {
        this.InitializeComponent();
        LoadCategoryIconView();
        if (model == null || model.ID == 0)
        {
            CurrentCategory = new CategoryModel
            {
                Name = "",
                Color = "#95a5a6",
                IconFile = ""
            };
        }
        else
        {
            CurrentCategory = model;
        }
    }

    public void LoadCategoryIconView()
    {
        var icons = new List<CategoryIconItem>
        {
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\headphone.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\musicalnote.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\keyboard.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\notebook.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\game.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\toolbox.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\world.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\star.png")
            },
            new CategoryIconItem()
            {
                IconFile=System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Assets\\CategoryIcon\\watermelon.png")
            }
        };
        CategoryIcon.ItemsSource = icons;

    }

    private void GridView_ItemClick(object sender, ItemClickEventArgs e)
    {
        var rect = (Microsoft.UI.Xaml.Shapes.Rectangle)e.ClickedItem;
        var color = ((SolidColorBrush)rect.Fill).Color;
        CurrentColor.Background = new SolidColorBrush(color);
        CurrentCategory.Color = color.ToString();

        // Delay required to circumvent GridView bug: https://github.com/microsoft/microsoft-ui-xaml/issues/6350
        Task.Delay(10).ContinueWith(_ => colorButton.Flyout.Hide(), TaskScheduler.FromCurrentSynchronizationContext());
    }

    private void CategoryIcon_ItemClick(object sender, ItemClickEventArgs e)
    {
        var icon = (CategoryIconItem)e.ClickedItem;
        CurrentCategory.IconFile = icon.IconFile.Replace(AppDomain.CurrentDomain.BaseDirectory, "");
    }
}
